package org.zxstudio.encrypt.utils;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;

public class EncryptUtil {
	
	public String getFileTime(long filetime) {
		SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); 
		String ftime =  formatter.format(new Date(filetime)); 
		return ftime;
	}
	
	public static String getFileSize(long filesize) {
		DecimalFormat df = new DecimalFormat("#.00");
		StringBuffer mstrbuf = new StringBuffer();

		if (filesize < 1024) {
			mstrbuf.append(filesize);
			mstrbuf.append(" B");
		} else if (filesize < 1048576) {
			mstrbuf.append(df.format((double) filesize / 1024));
			mstrbuf.append(" K");
		} else if (filesize < 1073741824) {
			mstrbuf.append(df.format((double) filesize / 1048576));
			mstrbuf.append(" M");
		} else {
			mstrbuf.append(df.format((double) filesize / 1073741824));
			mstrbuf.append(" G");
		}

		df = null;

		return mstrbuf.toString();
	}
	
	public Drawable getApkIcon(Context context,String path){      
		PackageManager pm = context.getPackageManager();      
		PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);      
		if(info != null){      
			ApplicationInfo appInfo = info.applicationInfo;

			if(Build.VERSION.SDK_INT >= 8){
				appInfo.sourceDir = path;
				appInfo.publicSourceDir = path;
			}

			return appInfo.loadIcon(pm);
		}   		
		return null;
	}
	
	public int getFileIcon(Context context,File f) {
		int icon = 0;

		String str = Constant.getFileIcon(f);
		if(str == null)
		{
			icon = -1;
		}
		else{
			Resources res = context.getResources();  
			icon =res.getIdentifier(str,"drawable",context.getPackageName());  

			if(icon <= 0 )
				icon = org.zxstudio.encrypt.R.drawable.icon_file;
		}

		return icon;
	}
}
